package app.servlets;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import app.utils.Utils;

import com.annvcit.imagewrite.CardInfo;
import com.annvcit.imagewrite.MyImageWriter;

/**
 * Servlet implementation class NextStep
 */
@WebServlet(name = "nextstep", urlPatterns = { "/nextstep" })
public class NextStep extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProc(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProc(request, response);
	}

	protected void doProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		String cardName = (String) request.getParameter("options");
		String fullName = (String) request.getParameter("fullname");
		String phone = (String) request.getParameter("phone");
		String email = (String) request.getParameter("email");
		String facebook = (String) request.getParameter("facebook");
		String website = (String) request.getParameter("website");
		String fax = (String) request.getParameter("fax");
		String address = (String) request.getParameter("address");
		String job = (String) request.getParameter("job");
		String organization = (String) request.getParameter("organization");
		String avatar = (String) session.getAttribute("avatar");

		session.removeAttribute("avatar");
		if (!Utils.isNull(cardName, fullName, phone, facebook, website, fax, address, job, organization, avatar)) {
			MyImageWriter writer = new MyImageWriter();
			CardInfo cardInfo = new CardInfo();
			cardInfo.setAddress(address);
			cardInfo.setAvatarPath(avatar);
			cardInfo.setCardName(cardName);
			cardInfo.setEmail(email);
			cardInfo.setFacebook(facebook);
			cardInfo.setFax(fax);
			cardInfo.setFullName(fullName);
			cardInfo.setJob(job);
			cardInfo.setOrganization(organization);
			cardInfo.setPhone(phone);
			cardInfo.setWebsite(website);

			File uploadFolder = new File(getServletContext().getRealPath("/card_upload/"));
			uploadFolder.mkdirs();
			writer.setOutputHomeFolder(uploadFolder.getAbsolutePath() + "/");
			String avatarPath = writer.createCard(cardInfo);
			
			session.setAttribute("avatarPath", "card_upload/" + (new File(avatarPath).getName()));
			session.setAttribute("imageLocation", avatarPath);
//			new File(avatar).delete();
			session.setAttribute("cardInfo", cardInfo);
			session.setAttribute("card_b", "images/cards/" + cardName + "_b.png");

			request.getRequestDispatcher("/next_step.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/create_card.jsp").forward(request, response);
		}

	}

}
